const user = require("../service/user");
const jwt = require("../extend/help");
const redis = require('../extend/redis'); // 验证码

// 注册接口
const Register = async (ctx) => {
    let res = await user.CheckPhone(ctx.request.body)
    if (res.length > 0) {
        return ctx.body = {
            code: 403,
            message: "账号已经被注册"
        }
    } else {
        let res = await user.register(ctx.request.body)
        if (res.affectedRows > 0) {
            return ctx.body = {
                code: 200,
                message: "账号注册成功"
            }
        } else {
            return ctx.body = {
                code: 403,
                message: "账号注册失败"
            }
        }
    }
}

// 登录接口
const login = async (ctx) => {
    ctx.verifyParams({
        phone: { type: "string", required: true },
        password: { type: "string", required: true },
    });
    let res = await user.getUserList(ctx.request.body);
    if (res.length) {
        let token = await jwt.signtoken(res[0]);
        let reds = await user.updateLoginTime(ctx.request.body);
        if (reds.affectedRows > 0) {
            return ctx.body = {
                data: {
                    code: 200,
                    data: res,
                    token: token,
                    msg: "登录成功",
                },
            }
        }
    } else {
        ctx.body = {
            code: 500,
            msg: "登录失败",
        };
    }
};

// 获取验证码接口
const PhoneCode = async (ctx) => {
    let { phone } = ctx.request.body;
    if (!phone) {
        return ctx.body = {
            code: 403,
            message: "手机号不能为空"
        }
    }
    let regphone = /^1[3-9]\d{9}$/;
    if (!regphone.test(phone)) {
        return ctx.body = {
            code: 403,
            message: "手机号不正确"
        }
    }
    let res = await user.CheckPhone(ctx.request.body);
    if (res.length == 0) {
        return ctx.body = {
            code: 403,
            message: "手机号未注册"
        }
    }
    let rediscode = await redis.get(phone)
    if (rediscode == null) {
        let code = Math.random().toFixed(6).slice(-6)
        await redis.setex(phone, 15, code)
        return ctx.body = {
            code: 200,
            message: '验证码已发送',
            data: code
        }
    } else {
        return ctx.body = {
            code: 403,
            message: '验证码发送频繁',
        }
    }
}

// 验证码登录接口
const codelogin = async (ctx) => {
    // 判断手机号有没有注册
    let res = await user.CheckPhone(ctx.request.body); // 参数 -- phone
    if (res.length == 0) {
        // 没有注册
        return ctx.body = {
            code: 403,
            message: "手机号未注册"
        }
    }
    let Phonecode = await redis.get(ctx.request.body.phone)
    let data = await user.codelogin(ctx.request.body);
    if (Phonecode == ctx.request.body.code) {
        let token = await jwt.signtoken(res[0])
        let reds = await user.updateLoginTime(ctx.request.body);
        if (reds.affectedRows > 0) {
            return ctx.body = {
                code: 200,
                message: "登录成功",
                data: data,
                token: token
            }
        }
    } else {
        return ctx.body = {
            code: 403,
            message: "验证码错误",
        }
    }
};

// 修改用户名
const uploadnickname = async (ctx) => {
    let res = await user.uploadnickname(ctx.request.body);
    // 无数据状态
    if (res.affectedRows > 0) {
        return ctx.body = {
            code: 200,
            message: '修改成功',
            data: res
        }
    } else {
        return ctx.body = {
            code: 403,
            message: '修改失败',
            data: res
        }
    }
}

// 修改密码
const uploadpassword = async (ctx) => {
    let res = await user.uploadpassword(ctx.request.body);
    // 无数据状态
    if (res.affectedRows > 0) {
        return ctx.body = {
            code: 200,
            message: '修改成功',
            data: res
        }
    } else {
        return ctx.body = {
            code: 403,
            message: '修改失败',
            data: res
        }
    }
}

// 修改头像
const uploadavatar = async (ctx) => {
    let res = await user.uploadavatar(ctx.request.body);
    if (res.affectedRows > 0) {
        return ctx.body = {
            code: 200,
            message: '修改成功',
            data: res
        }
    } else {
        return ctx.body = {
            code: 403,
            message: '修改失败',
            data: res
        }
    }
}

// 注销账号
const logoff = async (ctx) => {
    let res = await user.logoff(ctx.query);
    // 无数据状态
    if (res.affectedRows > 0) {
        return ctx.body = {
            code: 200,
            message: '删除成功',
            data: res
        }
    } else {
        return ctx.body = {
            code: 403,
            message: '删除失败',
            data: res
        }
    }
}

module.exports = {
    Register,// 注册接口
    login,// 登录接口
    PhoneCode,// 验证码登录接口
    codelogin,// 发送验证码接口
    uploadnickname,//修改用户名
    uploadpassword,//修改密码
    uploadavatar,//修改头像
    logoff,//注销账号
}